<?
	session_start();
	ob_start();
	//global $TRDF;
	if(empty($_SESSION['user_login'])){
		echo"
		<br><br><br><br><br>
		<div align=center style='font-family: verdana; font-size: 10px; color: red;'>
		 Maaf, Halaman ini tidak bisa diakses. <br>
		 <a href='../index.php'>Anda harus login terlebih dahulu.</a><br><br><BR>
		 <span style='color:black;'>Karyawan Dot Net</span>
		</div>
		";
		exit();
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Report:: Medical Restitusi</title>
<style type="text/css">
 body,table{
 	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
 }
</style>
<link rel="stylesheet" type="text/css" href="../style/print.css" media="print" />

<style type="text/css">
 .white_text{
 	background-color:#FFFFFF;
	color:#000000;
 }
</style>
</head>
<?
   	include("../lib/config.php");
	global $VAreaCode;
	
	$periode 	= $HTTP_GET_VARS['periode'];
	$jenis 		= $HTTP_GET_VARS['jenis'];
	$awal		= $HTTP_GET_VARS['awal'];
	// dd-mm-yyyy
	// 0123456789
	$d = substr($awal,0,2);
	$m = substr($awal,3,2);
	$y = substr($awal,6,4);
	
	$awal 		= $y."-".$m."-".$d;
	
	$akhir		= $HTTP_GET_VARS['akhir'];
	$dd = substr($akhir,0,2);
	$mm = substr($akhir,3,2);
	$yy = substr($akhir,6,4);
	$akhir 		= $yy."-".$mm."-".$dd;
	
	$divisi		= $HTTP_GET_VARS['division'];
	
	// cek hak akses untuk cabang
	$sad = mssql_query("SELECT nama_menu FROM VPrivUser A, VCabang B WHERE username='".$HTTP_SESSION_VARS['user_login']."' AND baca='1' AND A.nama_menu=B.Kode_Cabang",$conn);
	while($rad = mssql_fetch_array($sad)){
	// VARIABLE UNTUK CABANG
	$VAreaCode[] = $rad[0];
	}
	/////////////////////////////////
	
	// fungsi detail restitusi
	function CallMedical($conn,$jenis,$awal,$akhir,$VAreaCode,$divisi){
	
		switch($jenis){
			case "RJ":
				$MA 	= " and mata_anggaran in ('811101','811103','811199') ";
				$MSG 	= "RAWAT JALAN";
				break;
			case "RI":
				$MA = " AND mata_anggaran in ('811106') ";
				$MSG 	= "RAWAT INAP";
				break;
			case "PR":
				$MA = " AND mata_anggaran in ('811106') ";
				$MSG 	= "RAWAT PERSALINAN";
				break;
			case "KC":
				$MA = " AND mata_anggaran in ('811104') ";
				$MSG 	= "RAWAT KACAMATA";
				break;
		}
	
	  echo"<p><b><span style='font-size:14px;'>DATA $MSG</span></b></p>
	  <table width='100%' border='1' bordercolordark='#000000' bordercolorlight='black' style='border-collapse: collapse; background-color: white;'>
	   <tr>
		<td width='70'><b>NIK</td>
		<td><b>Nama</td>
		<td><b>Klaim</td>
		<td><b>Dibayar</td>
		<td><b>Ditolak</td>
		<td><b>Keterangan</td>
	   </tr>";
	
		$sdiv = "select kode_so,uraian from medicalrestitusidetail A, vlatestkarir B, vlevelcabang C where A.NIK=B.NIK ".$MA." and substring(C.kode_so,1,7)=substring(B.kode_so2,1,7) and tanggal between '".$awal."' and '".$akhir."' and A.status='A' AND C.kode_so=B.kode_so2 ";

///*		
		if($divisi != "ALL"){
			$sdiv .= " AND substring(B.kode_so2,1,7)='".substr($divisi,0,7)."'";
		}
		
		$sdiv .= "AND C.AreaCode IN (";
	
		for($k=0;$k<count($VAreaCode);$k++){
			$sdiv .= "'$VAreaCode[$k]',";
		}
	
		$sdiv .= "'$VAreaCode[0]') ";		
		
		$sdiv .= "group by kode_so,uraian order by uraian asc";
		
		$qdiv = mssql_query($sdiv,$conn);
		while($rdiv = mssql_fetch_array($qdiv)){
			echo"
			<tr>
			 <td colspan=6><b>$rdiv[uraian]</b></td>
			</tr>
			";
			// panggil data obat per divisi
			$sRJ = "select SUM(jml_diajukan) klaim, SUM(jml_disetujui) dibayar, SUM(jml_diajukan)-SUM(jml_disetujui) ditolak, B.NIK, Nama from medicalrestitusidetail A, vlatestkarir B, vlevelcabang C where A.NIK=B.NIK ".$MA." and substring(B.kode_so2,1,7)='".substr($rdiv[0],0,7)."' and tanggal between '$awal' and '$akhir' and B.kode_so2=C.kode_so ";
			
			if($divisi != "ALL"){
				$sRJ .= " AND substring(B.kode_so2,1,7)='".substr($divisi,0,7)."'";
			}
			
			$sRJ .= "AND C.AreaCode IN (";
		
			for($k=0;$k<count($VAreaCode);$k++){
				$sRJ .= "'$VAreaCode[$k]',";
			}
		
			$sRJ .= "'$VAreaCode[0]') ";		
			
			$sRJ .= " and A.status='A' group by B.NIK, Nama";
			$qRJ = mssql_query($sRJ,$conn);
			while($rRJ = mssql_fetch_array($qRJ))
			{
				echo"
				<tr>
				 <td>$rRJ[NIK]</td>
				 <td>$rRJ[Nama]</td>
				 <td align=right>".number_format($rRJ[klaim])."</td>
				 <td align=right>".number_format($rRJ[dibayar])."</td>
				 <td align=right>";
				 if(number_format($rRJ[ditolak]) == 0){
					echo"&nbsp;";
				 }
				 else{
					echo number_format($rRJ[ditolak]);
				 }
				 echo"</td>
				 <td>&nbsp;</td>
				</tr>";
			}
			// panggil jumlah data per divisi
			$SUMRJ = "select SUM(jml_diajukan) klaim, SUM(jml_disetujui) dibayar, SUM(jml_diajukan)-SUM(jml_disetujui) ditolak from medicalrestitusidetail A, vlatestkarir B, vlevelcabang C where A.NIK=B.NIK ".$MA." and substring(B.kode_so2,1,7)='".substr($rdiv[0],0,7)."' and tanggal between '$awal' and '$akhir' and A.status='A' and B.kode_so2=C.kode_so ";

			if($divisi != "ALL"){
				$SUMRJ .= " AND substring(B.kode_so2,1,7)='".substr($divisi,0,7)."'";
			}
			
			$SUMRJ .= "AND C.AreaCode IN (";
		
			for($k=0;$k<count($VAreaCode);$k++){
				$SUMRJ .= "'$VAreaCode[$k]',";
			}
		
			$SUMRJ .= "'$VAreaCode[0]') ";		
			
			$QSUMRJ = mssql_query($SUMRJ,$conn);
			$RSUMRJ = mssql_fetch_array($QSUMRJ);
			echo"
			<tr>
			 <td colspan=2 align=right><b>Total</b></td>
			 <td align=right><b>".number_format($RSUMRJ[klaim])."</td>
			 <td align=right><b>".number_format($RSUMRJ[dibayar])."</td>
			 <td align=right><b>";
			 if(number_format($RSUMRJ[ditolak]) == 0){
				echo"&nbsp;";
			 }
			 else{
				echo number_format($RSUMRJ[ditolak]);
			 }
			 echo"</td>
			 <td>&nbsp;</td>
			</tr>
			";
		}
			// panggil jumlah data klaim total
			$TSUMRJ = "select SUM(jml_diajukan) klaim, SUM(jml_disetujui) dibayar, SUM(jml_diajukan)-SUM(jml_disetujui) ditolak from medicalrestitusidetail A, vlatestkarir B, vlevelcabang C where A.NIK=B.NIK ".$MA." and tanggal between '$awal' and '$akhir'  and A.status='A' AND B.kode_so2=C.kode_so ";
			
			if($divisi != "ALL"){
				$TSUMRJ .= " AND substring(B.kode_so2,1,7)='".substr($divisi,0,7)."'";
			}
			
			$TSUMRJ .= "AND C.AreaCode IN (";
		
			for($k=0;$k<count($VAreaCode);$k++){
				$TSUMRJ .= "'$VAreaCode[$k]',";
			}
		
			$TSUMRJ .= "'$VAreaCode[0]') ";		
			
			$TQSUMRJ = mssql_query($TSUMRJ,$conn);
			$TRSUMRJ = mssql_fetch_array($TQSUMRJ);
			
		echo"
		<tr>
		<td colspan='2'><b>REKAPITULASI KLAIM $MSG </b></td>
		<td align='right'><b>".number_format($TRSUMRJ[klaim])."</td>
		<td align='right'><b>".number_format($TRSUMRJ[dibayar])."</td>
		<td align='right'><b>";
			 if(number_format($TRSUMRJ[ditolak]) == 0){
				echo"&nbsp;";
			 }
			 else{
				echo number_format($TRSUMRJ[ditolak]);
			 }
		echo"</td><td>&nbsp;</td></tr></table><br><br>";
    }
	
	// fungsi total pemakaian restitusi
	function CallTotalPemakaian($conn,$jenis,$awal,$akhir,$VAreaCode,$divisi){
	
		switch($jenis){
			case "RJ":
				$MA 	= " and mata_anggaran in ('811101','811103','811199') ";
				break;
			case "RI":
				$MA = " AND mata_anggaran in ('811106') ";
				break;
			case "PR":
				$MA = " AND mata_anggaran in ('811106') ";
				break;
			case "KC":
				$MA = " AND mata_anggaran in ('811104') ";
				break;
		}

		$sqx  = "select sum(jml_disetujui) from medicalrestitusidetail A, vlatestkarir B, vlevelcabang C where A.NIK=B.NIK AND B.kode_so2=C.kode_so ";
		
		$sqx .= "AND C.AreaCode IN (";
	
		for($k=0;$k<count($VAreaCode);$k++){
			$sqx .= "'$VAreaCode[$k]',";
		}
	
		$sqx .= "'$VAreaCode[0]') ";		
		
		if($divisi != "ALL"){
			$sqx .= " AND substring(B.kode_so2,1,7)='".substr($divisi,0,7)."'";
		}
		
		$sqx .= " AND tanggal between '".$awal."' and '".$akhir."' and A.status='A' ".$MA."";
		
		$sqy = mssql_query($sqx,$conn);
		$rqy = mssql_fetch_array($sqy);
		echo number_format($rqy[0]);
		
	}
?>
<body>
<table width="100%" border="0">
 <tr>
  <td align="left" style="font-size:14px;">KOPERASI KARYAWAN</td>
 </tr>
 <tr>
  <td align="left" style="font-size:14px;">
  </td>
 </tr>
 <tr>
  <td>Periode: <?=$HTTP_GET_VARS['awal']?> - <?=$HTTP_GET_VARS['akhir']?></td>
 </tr>
 <tr>
  <td align="right">Tgl Cetak: <?=date("d/m/Y")?></td>
 </tr> 
 <tr>
  <td><?=$sdiv?>
  <?
  // call function here
  $jenis = "RJ";
  CallMedical($conn,$jenis,$awal,$akhir,$VAreaCode,$divisi);
  
  $jenis = "RI";
  CallMedical($conn,$jenis,$awal,$akhir,$VAreaCode,$divisi);

  $jenis = "KC";
  CallMedical($conn,$jenis,$awal,$akhir,$VAreaCode,$divisi);

  $jenis = "PR";
  CallMedical($conn,$jenis,$awal,$akhir,$VAreaCode,$divisi);

  ?>
  </td>
 </tr>
 <tr>
  <td>
   <table width="100%">
    <tr>
     <td colspan="2">Pemakaian Periode Ini:</td>
    </tr>
    <tr>
     <td valign="top">
      <table>
        <tr>
         <td>&bull; Rawat Jalan</td>
         <? $jenis="RJ"; ?>
         <td align="right"><?=CallTotalPemakaian($conn,$jenis,$awal,$akhir,$VAreaCode,$divisi)?></td>
        </tr>
        <tr>
         <td>&bull; Rawat Inap</td>
         <? $jenis="RI"; ?>
         <td align="right"><?=CallTotalPemakaian($conn,$jenis,$awal,$akhir,$VAreaCode,$divisi)?></td>
        </tr>
        <tr>
         <td>&bull; Rawat Persalinan</td>
         <? $jenis="PR"; ?>
         <td align="right"><?=CallTotalPemakaian($conn,$jenis,$awal,$akhir,$VAreaCode,$divisi)?></td>
        </tr>
        <tr>
         <td>&bull; Rawat Kacamata</td>
         <? $jenis="KC"; ?>
         <td align="right"><?=CallTotalPemakaian($conn,$jenis,$awal,$akhir,$VAreaCode,$divisi)?></td>
        </tr>
      </table>
     </td>
     <td valign="top">
       <table align="right" border="0">
        <tr>
         <td colspan="3">Jakarta, ___________________________</td>
        </tr>
        <tr height="100">
         <td valign="bottom">___________________________</td>
         <td width="100">&nbsp;</td>
         <td valign="bottom">___________________________</td>
        </tr>
        <tr>
         <td>STAFF</td>
         <td width="100">&nbsp;</td>
         <td>MANAJER</td>
        </tr>
       </table>
     </td>
    </tr>
   </table>
  </td>
 </tr>
</table>
<?
//*///
if($HTTP_GET_VARS['excel'] == "yes"){
	header("Content-type: application/vnd.ms-excel");
	header("Content-Disposition: attachment; filename=LaporanPengobatan$title.xls");
	header("Pragma: no-cache");
	header("Expires: 0");
	}
else{

}
//	*/
?>
</body>
</html>
